package com.portal.cwegportal.controller;

import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import com.portal.cwegportal.input.addressbook.CreateAddressBookInput;
import com.portal.cwegportal.input.addressbook.DeleteAddressBookInput;
import com.portal.cwegportal.input.addressbook.EditAddressBookInput;
import com.portal.cwegportal.input.addressbook.GetAddressBookInput;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/portal/addressbook")
@Api(description ="通讯录API")
public class AddressBookController {
    @Autowired
    com.portal.cwegportal.service.AddressBookService addressBookService;

    @ApiOperation(value = "创建通讯录", notes = "创建通讯录", produces = "application/json")
    @PostMapping("/createAddressBook")
    public ResponseEntity<JsonResult> createAddressBook(@RequestBody CreateAddressBookInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"创建通讯录",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(addressBookService.createAddressBook(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "删除通讯录", notes = "删除通讯录", produces = "application/json")
    @PostMapping("/deleteAddressBook")
    public ResponseEntity<JsonResult> deleteAddressBook(@RequestBody DeleteAddressBookInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"删除通讯录",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(addressBookService.deleteAddressBook(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "修改通讯录", notes = "修改通讯录", produces = "application/json")
    @PostMapping("/editAddressBook")
    public ResponseEntity<JsonResult> editAddressBook(@RequestBody EditAddressBookInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"修改通讯录",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(addressBookService.editAddressBook(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "查詢通讯录", notes = "查詢通讯录", produces = "application/json")
    @PostMapping("/getAddressBookPerson")
    public ResponseEntity<JsonResult> getAddressBookPerson(@RequestBody GetAddressBookInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"查詢通讯录",input);
        JsonResult r = new JsonResult();

        try {
                r.setResult(addressBookService.getAddressBookPerson(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "通过id查询人", notes = "通过id查询人", produces = "application/json")
    @PostMapping("/getAddressBookById")
    public ResponseEntity<JsonResult> getAddressBookById(@RequestBody GetAddressBookInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"通过id查询通讯录",input);
        JsonResult r = new JsonResult();

        try {
            r.setResult(addressBookService.getAddressBookById(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "查询通讯录", notes = "查询通讯录", produces = "application/json")
    @PostMapping("/getAddressBookOrg")
    public ResponseEntity<JsonResult> getAddressBookOrg(@RequestBody GetAddressBookInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        BusinessLogger.Info( LoggerGenre.Add,"查询通讯录",input);
        JsonResult r = new JsonResult();
        try {
            r.setResult(addressBookService.getAddressBookOrg(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

}
